package com.jeesite.modules.digitalize.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeesite.modules.digitalize.constants.RelationTypeEnum;
import com.jeesite.modules.digitalize.dao.DigitalizeBindDao;
import com.jeesite.modules.digitalize.entity.DigitalizeBind;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 关联关系表(DigitalizeBind)表服务实现类
 *
 * @author mxz
 * @since 2023-11-15 15:52:09
 */
@Service("digitalizeBindService")
public class DigitalizeBindService extends ServiceImpl<DigitalizeBindDao, DigitalizeBind> {


    /**
     * 删除旧数据新增新数据
     * @param fatherId
     * @param sonIdList
     * @param type
     */
    public void deleteAndAdd(Long fatherId, List<Integer> sonIdList, RelationTypeEnum type) {
        this.lambdaUpdate().eq(DigitalizeBind::getFatherId, fatherId).eq(DigitalizeBind::getType, type.getCode()).remove();
        if (CollectionUtil.isNotEmpty(sonIdList)) {
            List<DigitalizeBind> collect = sonIdList.stream().map(item -> new DigitalizeBind().setSonId(item).setFatherId(fatherId).setType(type.getCode())).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }

}

